<template>
  <div>
    <div v-if="shows == 'list'">
      <Header>
        <el-button size="small" type="primary" @click="add">
          新建奖品
        </el-button>
      </Header>
      <Main :data="listData" :footer-lock="totals > 10">
        <el-table-column
          label="奖品名称"
          align="center"
          property="goods.goods_name"
        ></el-table-column>
        <el-table-column label="奖品封面" align="center" property="ssid">
          <template slot-scope="scope">
            <img
              :src="
                $store.state.qiniu +
                  scope.row.sku.cover +
                  $store.state.imgRotate
              "
              width="80px"
              height="80px"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="库存"
          align="center"
          property="sku.sku_stock"
        ></el-table-column>
        <el-table-column
          label="价值"
          align="center"
          property="money"
        ></el-table-column>

        <el-table-column
          label="操作"
          align="center"
          fixed="right"
          width="250"
          class-name="tableoperation"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="del(scope.row.prize_id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </Main>
      <Footer :total="totals">
        <el-pagination
          :page-size="10"
          layout="prev, pager, next"
          :total="totals"
          @current-change="changes"
        >
        </el-pagination>
      </Footer>
    </div>
    <Add v-if="shows == 'add'" @out="out" />
  </div>
</template>
<script>
import { PRIZEDEPOT, DELPRIZE } from "../service";

export default {
  components: {
    Add: () => import("./form")
  },
  data() {
    return {
      shows: "list",
      listData: [],
      totals: 0,
      pages: 1
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async del(id) {
      await this.$confirm("是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          DELPRIZE({ prize_id: id }).then(res => {
            let { code, msg } = res;
            if (code === 200) {
              this.getList();
            }
            this.$message({
              message: msg,
              type: code === 200 ? "success" : "error"
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除!"
          });
        });
    },
    async getList() {
      await PRIZEDEPOT({
        page: this.pages,
        pageshow: 10
      }).then(res => {
        let { code, data, msg } = res;
        if (code === 200) {
          this.listData = data.list;
          this.totals = data.count;
        } else {
          this.$message.error(msg);
          this.listData = [];
        }
      });
    },
    add() {
      this.shows = "add";
    },
    out() {
      this.shows = "list";
      this.getList();
    },
    changes(page) {
      this.pages = page;
      this.getList();
    }
  }
};
</script>
